package com.qf.java2107.yb.hotel.service.impl;

import com.qf.java2107.yb.hotel.common.ResultVO;
import com.qf.java2107.yb.hotel.constant.MessageConstant;
import com.qf.java2107.yb.hotel.dao.FoodDao;
import com.qf.java2107.yb.hotel.entity.BeanFactory;
import com.qf.java2107.yb.hotel.pojo.Food;
import com.qf.java2107.yb.hotel.pojo.FoodType;
import com.qf.java2107.yb.hotel.service.FoodService;
import org.apache.commons.beanutils.BeanUtils;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author yb
 * @version 1.0
 * @date 2021-10-26
 **/
public class FoodServiceImpl implements FoodService {

    private FoodDao foodDao = (FoodDao) BeanFactory.getBean("foodDao");


    @Override
    public void save(Food food) {
        try {
            foodDao.save(food);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public ResultVO finById(Integer id){
        try {
            Food food = foodDao.findById(id);
           return ResultVO.ok(MessageConstant.FOODTYPE_QUERY_SUCCESS,food);
        } catch (SQLException e) {
            e.printStackTrace();
            return ResultVO.error(MessageConstant.FOODTYPE_QUERY_FAIL);
        }
    }

    @Override
    public ResultVO foodAdd(Food food) {

        try {
            int i = foodDao.foodAdd(food);
            if (1==i){
                return ResultVO.ok(MessageConstant.FOOD_ADD_SUCCESS);
            }
        } catch (SQLException e) {
            e.printStackTrace();

        }
        return ResultVO.error(MessageConstant.FOOD_ADD_FAIL);
    }

    @Override
    public ResultVO list() {
        try {
            List<Food> foods = new ArrayList<>();
            List<Map<String, Object>> all = foodDao.findAll();
            for (Map<String, Object> map : all) {
                Food food = new Food();
                BeanUtils.populate(food,map);
                FoodType foodType = new FoodType();
                foodType.setTypeId((Integer) map.get("typeId"));
                foodType.setTypeName((String) map.get("typeName"));
                food.setFoodType(foodType);
                foods.add(food);

            }
            return ResultVO.ok(MessageConstant.FOOD_ADD_SUCCESS,foods);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ResultVO.error(MessageConstant.FOOD_ADD_FAIL);
    }

    @Override
    public ResultVO delete(Integer foodId) {

        try {
            int delete = foodDao.delete(foodId);
            if (1==delete){
                return ResultVO.ok("删除成功",foodId);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ResultVO.error("删除失败");
    }

    @Override
    public ResultVO findLikeFoodName(String foodName) {
        try {
             foodDao.findLikeName(foodName);
            return ResultVO.ok(MessageConstant.FOODTYPE_QUERY_SUCCESS);

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ResultVO.error("查询失败");
    }

    @Override
    public ResultVO findByFoodName(String foodName) {
        return null;
    }

    @Override
    public ResultVO update(Food food) {

        try {
            foodDao.updateFood(food);
            return ResultVO.ok(MessageConstant.FOODTYPE_QUERY_SUCCESS);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ResultVO.error("更新失败");
    }
}
